package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamSyllabusVo;
import com.ruoyi.basice_info.mapper.TsExamSyllabusVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbAssessmentMethodMapper;
import com.ruoyi.basice_info.domain.TbAssessmentMethod;
import com.ruoyi.basice_info.service.ITbAssessmentMethodService;

/**
 * 考核方式设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-07
 */
@Service
public class TbAssessmentMethodServiceImpl implements ITbAssessmentMethodService
{
    @Autowired
    private TbAssessmentMethodMapper tbAssessmentMethodMapper;

    @Autowired
    private TsExamSyllabusVoMapper tsExamSyllabusVoMapper;
    /**
     * 查询考核方式设置
     *
     * @param assessmentMethodId 考核方式设置主键
     * @return 考核方式设置
     */
    @Override
    public TbAssessmentMethod selectTbAssessmentMethodByAssessmentMethodId(Long assessmentMethodId)
    {
        return tbAssessmentMethodMapper.selectTbAssessmentMethodByAssessmentMethodId(assessmentMethodId);
    }

    @Override
    public TbAssessmentMethod getTbAssessmentMethodByAssessmentMethodName(String name) {
        return tbAssessmentMethodMapper.getTbAssessmentMethodByAssessmentMethodName(name);
    }

    @Override
    public TbAssessmentMethod getTbAssessmentMethodByAssessmentMethodCode(String code) {
        return tbAssessmentMethodMapper.getTbAssessmentMethodByAssessmentMethodCode(code);
    }

    /**
     * 查询考核方式设置列表
     *
     * @param tbAssessmentMethod 考核方式设置
     * @return 考核方式设置
     */
    @Override
    public List<TbAssessmentMethod> selectTbAssessmentMethodList(TbAssessmentMethod tbAssessmentMethod)
    {
        return tbAssessmentMethodMapper.selectTbAssessmentMethodList(tbAssessmentMethod);
    }

    /**
     * 新增考核方式设置
     *
     * @param tbAssessmentMethod 考核方式设置
     * @return 结果
     */
    @Override
    public int insertTbAssessmentMethod(TbAssessmentMethod tbAssessmentMethod)
    {
        return tbAssessmentMethodMapper.insertTbAssessmentMethod(tbAssessmentMethod);
    }

    /**
     * 修改考核方式设置
     *
     * @param tbAssessmentMethod 考核方式设置
     * @return 结果
     */
    @Override
    public int updateTbAssessmentMethod(TbAssessmentMethod tbAssessmentMethod)
    {
        return tbAssessmentMethodMapper.updateTbAssessmentMethod(tbAssessmentMethod);
    }

    /**
     * 批量删除考核方式设置
     *
     * @param assessmentMethodIds 需要删除的考核方式设置主键
     * @return 结果
     */
    @Override
    public int deleteTbAssessmentMethodByAssessmentMethodIds(Long[] assessmentMethodIds)
    {
        int num = -1;
        int nums = 0;
        List<Long> longList = Arrays.asList(assessmentMethodIds);
        for (Long item: longList) {
            TsExamSyllabusVo syllabus = new TsExamSyllabusVo();
//            syllabus.setSemesterId(item);
            syllabus.setAssessmentMethodId(item);
            // 去子表中查询一下
            List<TsExamSyllabusVo> syllabusList = tsExamSyllabusVoMapper.selectTsExamSyllabusList(syllabus);
            // 判断一下是否有有元素
            if(syllabusList.size()<=0) {
                // 在子表中不存在则删除
                num = tbAssessmentMethodMapper.deleteTbAssessmentMethodByAssessmentMethodId(item);
            } else {
                nums ++;
            }
        }
        if(longList.size() == 1) {
            if(nums>0) {
                throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
            } else  {
                if (nums > 0) {
                    throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
                }
            }
        }
//
//        return tbAssessmentMethodMapper.deleteTbAssessmentMethodByAssessmentMethodIds(assessmentMethodIds);
        return num;

    }

    /**
     * 删除考核方式设置信息
     *
     * @param assessmentMethodId 考核方式设置主键
     * @return 结果
     */
    @Override
    public int deleteTbAssessmentMethodByAssessmentMethodId(Long assessmentMethodId)
    {
        return tbAssessmentMethodMapper.deleteTbAssessmentMethodByAssessmentMethodId(assessmentMethodId);
    }
}
